#import "UpLoadImage.h"
#import "ASIFormDataRequest.h"


@interface UpLoadImage()<ASIHTTPRequestDelegate,ASIProgressDelegate>
@end

@implementation UpLoadImage


@synthesize delegate;

-(void) upLoadImages:(UIImage*) image {
    
    NSMutableDictionary* tempDic = [[NSMutableDictionary alloc] init];
//    
//    ETUrlEngine* tempUrlEngine = [[ETUrlEngine alloc] init];
//    [tempUrlEngine setNeedUserSession: YES];
//    NSDictionary* tempApi3Dic = [tempUrlEngine getPostDicByAPI3SignByTypeWithPartmentAndData:tempDic date:nil];
//    
//    NSURL *nsUrl = [[NSURL alloc] initWithString:[tempUrlEngine httpHeadUrl]];
//    ASIFormDataRequest* request = [[ASIFormDataRequest alloc] initWithURL:nsUrl];
//    
//    [request setDelegate:self];
//    [request setUploadProgressDelegate:self];
//    
//    //别忘记showAccurateProgress也要设置为YES (默认为NO,则只显示0%和100%):
//    request.showAccurateProgress=YES;//
//    
//    //UIImage* image = [[myShopPhontos objectAtIndex:0] image];
//    NSData* data = UIImageJPEGRepresentation(image, 0.80);
//    
//    [tempApi3Dic setValue:data forKey:@"imagefile"];
//    
//    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:[[tempApi3Dic allKeys] count]];
//    
//    for (NSString *key in [tempApi3Dic keyEnumerator]) {
//        NSObject *val = [tempApi3Dic objectForKey:key];
//        
//        if ([val isKindOfClass:[NSData class]]) {
//            request.postFormat = ASIMultipartFormDataPostFormat;
//            [request addData:data withFileName:@"image.jpg" andContentType:@"image/jpeg" forKey:key];
//        } else {
//            [request addPostValue:val forKey:key];
//            [arr addObject:[NSString stringWithFormat:@"%@=%@", key, val]];
//        }
//        
//    }
    
    
//    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
//    
//    [request startAsynchronous]; //异步执行
}


- (void)requestFinished:(ASIHTTPRequest *)request {
    
    NSData *data = [NSData dataWithData:[request responseData]]; 	
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingUTF8);
    NSString* temPStr = [[NSString alloc] initWithData:data encoding:enc]; 
    
    if (delegate && [delegate respondsToSelector:@selector(postDataFinished:)]) {
        [delegate performSelector:@selector(postDataFinished)withObject:temPStr];
    }

}


- (void)requestFailed:(ASIHTTPRequest *)request {
    
    NSData *data = [NSData dataWithData:[request responseData]]; 	
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingUTF8);
    NSString* temPStr = [[NSString alloc] initWithData:data encoding:enc]; 
    
    if (delegate && [delegate respondsToSelector:@selector(postDataFinished:)]) {
        [delegate performSelector:@selector(postDataFailed)withObject:temPStr];
    }
}


- (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes {
    
}


- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength {
     NSLog(@"totalupload:%lld",newLength);
    
    NSNumber* tempNum = [[NSNumber alloc] initWithDouble:(newLength/[request postLength])];
    
    if (delegate && [delegate respondsToSelector:@selector(postDataProgress:)]) {
        [delegate performSelector:@selector(postDataProgress)withObject:tempNum];
    }
}



@end
